package com.haibei.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import android.content.Context;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.haibei.R;
import com.haibei.adapter.bean.Article;
import com.haibei.adapter.bean.Topic;

public class SearchAdapter extends BaseAdapter {
	private Context context;
	private ArrayList<Article> mArrayListArticle;
	private String keyWord;
	private int type;

	public SearchAdapter(Context context, ArrayList mArrayList, String keyword, int type) {
		this.context = context;
		this.mArrayListArticle = mArrayList;
		this.keyWord = keyword;
		this.type = type;
	}

	@Override
	public int getCount() {
		return mArrayListArticle.size();
	}

	@Override
	public Object getItem(int position) {
		return mArrayListArticle.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder vl = null;
		if (convertView == null) {
			convertView = View.inflate(context, R.layout.item_search, null);
			vl = new ViewHolder();
			vl.textViewTitle = (TextView) convertView.findViewById(R.id.textViewTitle);
			vl.textViewDate = (TextView) convertView.findViewById(R.id.textViewDate);
			vl.textViewFrom = (TextView) convertView.findViewById(R.id.textViewFrom);
			convertView.setTag(vl);
		} else {
			vl = (ViewHolder) convertView.getTag();
		}
		Object bean = mArrayListArticle.get(position);
		SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日");
		Calendar ca = Calendar.getInstance();

		if (1 == type) {
			Article article = (Article) bean;
			String title = article.getTitle();
			vl.textViewTitle.setText(Html.fromHtml(createColorTitle(title, keyWord)));
			ca.setTimeInMillis(Long.parseLong(article.getPublishTime()));
			Date d = ca.getTime();
			vl.textViewDate.setText(sdf.format(d));
			vl.textViewFrom.setText(article.getOrigin());
		} else {
			Topic topic = (Topic) bean;
			String title = topic.getTitle();
			vl.textViewTitle.setText(Html.fromHtml(createColorTitle(title, keyWord)));
			ca.setTimeInMillis(Long.parseLong(topic.getPublishTime()));
			Date d = ca.getTime();
			vl.textViewDate.setText(sdf.format(d));
			vl.textViewFrom.setText(topic.getOrigin());
		}

		return convertView;
	}

	private String createColorTitle(String title, String keyword) {
		int index = title.indexOf(keyWord);
		if (-1 != index) {
			String head = title.substring(0, index);
			String htmlText = "<font color=\"#FF0000\">" + keyWord + "</font>";
			int keywordLen = keyWord.length();
			String tail = title.substring(index + keywordLen, title.length() - 1);
			StringBuffer sbf = new StringBuffer();
			sbf.append(head + htmlText + tail);
			return sbf.toString();
		} else {
			return keyword;
		}
	}

	class ViewHolder {
		TextView textViewTitle;
		TextView textViewDate;
		TextView textViewFrom;
	}
}
